/*
 * INSANE - Interactive Structural Analysis Environment
 *
 * Copyright (C) 2003-2006
 * Universidade Federal de Minas Gerais
 * Escola de Engenharia
 * Departamento de Engenharia de Estruturas
 *
 * Author's email :     insane@dees.ufmg.br
 * Author's Website :   http://www.dees.ufmg.br/insane
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 */
 
package br.ufmg.dees.insane.draw;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.util.Vector;

/**
 * @author Fonseca, Flavio & Pitangueira, Roque
 * @since	June 2004
 */
public abstract class GridDisplaceDraw extends Draw {
	
	private double value = 0;
	
//*****************************************************************************
	
	public void setValue(double value) {
		this.value = value;
	}
	
//*****************************************************************************
	
	public double getValue() {
		return value;
	}
	
//*****************************************************************************
//*****************************************************************************
	
	public static class Linear extends GridDisplaceDraw {
	
	//*****************************************************************************
		
		public Linear() {
			this( new Point(0, 0) );
		}
		
	//*****************************************************************************
		
		public Linear(Point loc) {
			this(loc, 0);
		}
		
	//*****************************************************************************
		
		public Linear(Point loc, double theta) {
			this(loc, theta, null);
		}
		
	//*****************************************************************************
		
		public Linear(Point loc, double theta, Object represents) {
			this.setLocation(loc);
			this.setAngle(theta);
			this.setRepresents(represents);
			this.setSize(new Dimension(18, 10));
		}
		
	//*****************************************************************************
		
		public Vector<Object> getShapes() {
			
			Vector<Object> shapes = new Vector<Object>();
			
			int x = this.getLocation().x;
			int y = this.getLocation().y;
			
			double sc = this.getScale();
			int MOD = (int)(15*sc);
			int D = (int)(12*sc);
			int t = (int)(D/(2*Math.sqrt(2)));
			
			if (this.getValue() > 0 && sc!=0) {
				
				shapes.add(new Ellipse2D.Double(x-D/2, y-D/2, D, D));
				shapes.add(new Line2D.Double(x+t-MOD, y+t-MOD, x-t, y-t));
				shapes.add(new Ellipse2D.Double(x-D/2-MOD, y-D/2-MOD, D, D));
				
				shapes.add(new Line2D.Double(x-t-MOD, y-t-MOD, x+t-MOD, y+t-MOD));
				shapes.add(new Line2D.Double(x-t-MOD, y+t-MOD, x+t-MOD, y-t-MOD));
				
			}else if (this.getValue() < 0 && sc!=0) {
				
				shapes.add(new Ellipse2D.Double(x-D/2, y-D/2, D, D));
				shapes.add(new Line2D.Double(x+t-MOD, y+t-MOD, x-t, y-t));
				shapes.add(new Ellipse2D.Double(x-D/2-MOD, y-D/2-MOD, D, D));
				
				shapes.add(new Ellipse2D.Double(x-MOD-3, y-MOD-3, 6, 6));
				
			}
			
			return shapes;
			
		}
		
		//*****************************************************************************
		
		/**
		 * Returns the draw bounds in device units.
		 *
		 * @return the draw bounds
		 */
		public Rectangle getBounds() {
			
			int x = this.getLocation().x;
			int y = this.getLocation().y;
			int width = this.getSize().width;
			int height = this.getSize().height;
			double scale = this.getScale();
			
			int W = (int)(width * scale);
			int H = (int)(height * scale);
			return new Rectangle(x - W - 1, y - H/2 - 1, W + 1, H + 1);
		}
		
	//*****************************************************************************
		
	}
	
//*****************************************************************************
//*****************************************************************************
	
	public static class Angular extends GridDisplaceDraw {
		
	//*****************************************************************************
		
		public Angular() {
			this(new Point(0, 0));
		}
		
	//*****************************************************************************
		
		public Angular(Point loc) {
			this(loc, 0);
		}
		
	//*****************************************************************************
		
		public Angular(Point loc, double theta) {
			this(loc, theta, 0);
		}
		
	//*****************************************************************************
		
		public Angular(Point loc, double theta, double value) {
			this(loc, theta, value, null);
		}
		
	//*****************************************************************************
		
		public Angular(Point loc, double theta, double value, Object represents) {
			this.setLocation(loc);
			this.setAngle(theta);
			this.setValue(value);
			this.setRepresents(represents);
			this.setSize(new Dimension(8, 8));
		}
		
	//*****************************************************************************
		
		public boolean equals(Object obj) {
			if (super.equals(obj) && obj instanceof GridDisplaceDraw.Angular) {
				GridDisplaceDraw.Angular d = (GridDisplaceDraw.Angular)obj;
				if (d.getValue() == getValue()){
					return true;
				}
			}
			return false;
		}
		
	//*****************************************************************************
		
		public Vector<Object> getShapes() {
			
			Vector<Object> shapes = new Vector<Object>();
			
			int x = this.getLocation().x;
			int y = this.getLocation().y;
			int height = this.getSize().height;
			if (height<10) height=10;
			double scale = this.getScale();
			double h = 2*height/5 * scale;
			if (h<3) h=3;
			//if (h>6) h=6;
			
			//double MOD = width * scale;
			double MOD = 20 * scale;
			//if (MOD<18) MOD=18;
			double ARROW = 6*scale;
			
			if (this.getValue() > 0) {
				shapes.add(new Line2D.Double(x + MOD, y+height*scale, x + MOD - ARROW, y+height*scale + (ARROW/2)));
				shapes.add(new Line2D.Double(x + MOD, y+height*scale, x + MOD - ARROW, y+height*scale - (ARROW/2)));
				shapes.add(new Line2D.Double(x + MOD - ARROW, y+height*scale, x + MOD - 2*ARROW, y+height*scale + (ARROW/2)));
				shapes.add(new Line2D.Double(x + MOD - ARROW, y+height*scale, x + MOD - 2*ARROW, y+height*scale - (ARROW/2)));
				shapes.add(new Line2D.Double(x, y+height*scale, x + MOD, y+height*scale));
				shapes.add(new Line2D.Double(x, y, x, y+1.5*height*scale));
				shapes.add(new Line2D.Double(x + MOD, y+h/2, x + MOD, y+1.5*height*scale));
				shapes.add(new Ellipse2D.Double(x + MOD - (0.5*h), y-h/2, h, h) );
			}
			if (this.getValue() < 0) {
				shapes.add(new Line2D.Double(x - MOD, y+height*scale, x - MOD + ARROW, y+height*scale + (ARROW/2)));
				shapes.add(new Line2D.Double(x - MOD, y+height*scale, x - MOD + ARROW, y+height*scale - (ARROW/2)));
				
				shapes.add(new Line2D.Double(x - MOD + ARROW, y+height*scale, x - MOD + 2*ARROW, y+height*scale + (ARROW/2)));
				shapes.add(new Line2D.Double(x - MOD + ARROW, y+height*scale, x - MOD + 2*ARROW, y+height*scale - (ARROW/2)));
				
				shapes.add(new Line2D.Double(x, y+height*scale, x - MOD, y+height*scale));
				shapes.add(new Line2D.Double(x, y, x, y+1.5*height*scale));
				shapes.add(new Line2D.Double(x - MOD, y+h/2, x - MOD, y+1.5*height*scale));
				shapes.add(new Ellipse2D.Double(x - MOD - (0.5*h), y-h/2, h, h) );
				
			}
			
			return shapes;
			
		}
		
		//*****************************************************************************
		
		/**
		 * Returns the draw bounds in device units.
		 *
		 * @return the draw bounds
		 */
		public Rectangle getBounds() {
			
			int x = this.getLocation().x;
			int y = this.getLocation().y;
			int W = this.getSize().width * 4;
			int H = this.getSize().height * 4;
			
			return new Rectangle((int)(x - W/2), (int)(y - H/2), W, H);
		}
		
	//*****************************************************************************
		
	}
	
//*****************************************************************************
//*****************************************************************************
}
